package com.bw.goods.mvvm.viewmodel;

import android.content.Intent;
import android.os.Bundle;

import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bw.goods.mvvm.adapter.GoodDetailAdapter;
import com.bw.goods.mvvm.model.HomeValuesModel;
import com.bw.goods.mvvm.model.entity.AddShopEntity;
import com.bw.goods.mvvm.model.entity.GoodDetailEntity;
import com.bw.goods.mvvm.model.entity.HomeGoodsEntity;
import com.bw.goods.mvvm.model.entity.Listdetail;
import com.bw.mvvm_common.BaseApplication;
import com.bw.mvvm_common.Code;
import com.bw.mvvm_common.dao.GoodBean;
import com.bw.mvvm_common.dao.GoodBeanDao;
import com.bw.mvvm_common.dao.UserBean;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.field.UIField;
import com.bw.mvvm_common.mvvm.utils.GreenDaoUtils;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public
/**
 * @author Lishaohe
 * @description:
 * @date :2021/10/20 0020 下午 7:24
 *
 */
class HomeValuesViewmodel extends BaseViewModel<HomeValuesModel> {
    public BaseQuickAdapter adapter = new GoodDetailAdapter();
    GoodDetailEntity.DataBean data;
   public ObservableField<String> img   = new ObservableField<>();
   public ObservableField<String> title = new ObservableField<>();
   public ObservableField<Integer> money = new ObservableField<>();
   public ObservableField<String> detail = new ObservableField<>();
    int goodsid;
    int user_id;
    List<UserBean> list;
    @Override
    public void onCreate() {
        super.onCreate();

        list = GreenDaoUtils.getGreenDaoUtils().green().queryBuilder().list();


        HashMap<String, Object> map = new HashMap<>();
        map.put("goodsId",goodsid);
        map.put("detail",Code.DETAIL);
        m.requestGoodDetail(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);

        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("categoryId",14);
        map1.put("pageNo",1);
        map1.put("goods", Code.GOODS);
        m.requestGoods(map1)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);
    }

    public void requestDetail(int id){
        goodsid = id;
    }

    @Override
    protected void result(BaseEntity entity) {

        if (entity instanceof GoodDetailEntity){
            GoodDetailEntity detailEntity = (GoodDetailEntity) entity;
            data = detailEntity.getData();
            img.set(data.getGoods_default_icon());
            title.set(data.getGoods_desc());
            money.set(data.getGoods_stock_count());
            detail.set(data.getGoods_default_sku());
//
        }else if (entity instanceof HomeGoodsEntity){
            HomeGoodsEntity homeGoodsEntity = (HomeGoodsEntity) entity;
            adapter.setNewInstance(homeGoodsEntity.getData());
        }else if ( entity instanceof AddShopEntity){
            AddShopEntity addShopEntity = (AddShopEntity) entity;
            if (addShopEntity.getMessage().equals("添加购物车成功")){
                ToastUtils.showLong("添加购物车成功");
           }else {
                ToastUtils.showLong("sb");
            }
        }


    }


    //返回
    public void finishActivity(){
        finishPage();
    }
    //分享
    public void share(){
    }

    //购物车按钮
    public void carList(){
    }
    //加入购物车
    public void addCar(){
        if (list.isEmpty()){
            ToastUtils.showLong("未登录加入数据库购物车");
            GoodBeanDao good = GreenDaoUtils.getGreenDaoUtils().good();
            GoodBean goodBean = new GoodBean();
            goodBean.setGoodsCount(1);
            goodBean.setGoodsDesc(data.getGoods_desc());
            goodBean.setGoodsIcon(data.getGoods_default_icon());
            goodBean.setGoodsId(data.getId());
            goodBean.setGoodsPrice(data.getGoods_default_price());
            goodBean.setGoodsSku(data.getGoods_default_sku());
            goodBean.setTime((int)System.currentTimeMillis());
            good.insert(goodBean);

        }else {
            user_id = list.get(0).getUser_id();
            HashMap<String,Object> car = new HashMap<>();
            car.put("car",Code.CAR);
            car.put("goodsCount",1);
            car.put("goodsDesc",data.getGoods_desc());
            car.put("goodsIcon",data.getGoods_default_icon());
            car.put("goodsId",data.getId());
            car.put("goodsPrice",data.getGoods_default_price());
            car.put("goodsSku",data.getGoods_default_sku());
            car.put("time",(int)System.currentTimeMillis());
            car.put("userId",user_id);
            m.requestshopcar(car)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(this);

        }

    }
}
